/**
 * @param {Object} Subject 被观察者对象
 * @param {Object} Observer 观察者对象
 * @description 
 *	观察者模式是一种允许对象监听和响应事件的设计模式。该模式有一个被观察者对象，它会在发生事件时通知观察者对象
 */
class Subject {
	constructor() {
		this.observers = []; //观察者列表
	}

	addObserver(observer) {
		this.observers.push(observer);
	}

	removeObserver(observer) {
		let index = this.observers.indexOf(observer);
		if (index !== -1) {
			this.observers.splice(index, 1);
		}
	}

	notify(data) {
		for (let observer of this.observers) {
			observer.update(data);
		}
	}
}

class Observer {
	constructor() {}

	update(data) {
		console.log(`Data has changed to ${data}`);
	}
}

let subject = new Subject();
let observer1 = new Observer();
let observer2 = new Observer();

subject.addObserver(observer1);
subject.addObserver(observer2);

subject.notify("some data has changed");
// 输出：
// Data has changed to some data has changed
// Data has changed to some data has changed

subject.removeObserver(observer2);
subject.notify("other data has changed");
// 输出：
// Data has changed to other data has changed